package com.hh.system.action;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hh.baseframework.util.document.ExcelUtil;
import com.hh.baseframework.util.document.ExportSetInfo;
import com.hh.baseframework.util.json.Json;
import com.hh.system.inf.IFileAction;
import com.hh.system.util.base.BaseServletAction;

@SuppressWarnings("serial")
public class ExcelAction extends BaseServletAction  implements IFileAction{
	private byte[] bytes = null;
	private String headNames = "";
	private String fieldNames = "";
	private String title;
	private String dataListString;

	public String exportExcel() {
		List<Map<String, Object>> dataList = Json.toMapList(dataListString);
		if (title == null) {
			title = "test";
		}
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		List<String[]> headNameList = new ArrayList<String[]>();
		headNameList.add(headNames.split(","));
		List<String[]> fieldNameList = new ArrayList<String[]>();
		fieldNameList.add(fieldNames.split(","));
		ExportSetInfo setInfo = new ExportSetInfo();
		Map<String, List<Map<String, Object>>> map = new HashMap<String, List<Map<String, Object>>>();
		map.put(title, dataList);
		setInfo.setObjsMap(map);
		setInfo.setFieldNames(fieldNameList);
		setInfo.setTitles(new String[] { title });
		setInfo.setHeadNames(headNameList);
		setInfo.setOut(baos);
		try {
			ExcelUtil.export2Excel(setInfo);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}

		this.setBytes(baos.toByteArray());
		return "excel";
	}

	public byte[] getBytes() {
		return bytes;
	}

	public void setBytes(byte[] bytes) {
		this.bytes = bytes;
	}


	public int getContentLength() {
		return this.bytes.length;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDataListString() {
		return dataListString;
	}

	public void setDataListString(String dataListString) {
		this.dataListString = dataListString;
	}

	public String getHeadNames() {
		return headNames;
	}

	public void setHeadNames(String headNames) {
		this.headNames = headNames;
	}

	public String getFieldNames() {
		return fieldNames;
	}

	public void setFieldNames(String fieldNames) {
		this.fieldNames = fieldNames;
	}


}
